<?php
/**
 * 配送方式模型
 * @author cjli
 *
 */
class ShippingModel extends Model
{
	/**
	 * 获取配送方式列表
	 */
	public static function get_shipping_list()
	{
		global $language_id;
		$cacheId = 'shipping_list_'.$language_id;
		if(S($cacheId)){
			$shippingList = S($cacheId);
		}else{
			$shippingList = null;
			$shipping_db = D('shipping');
			$where = array(
				'status' => 1,
				'language_id' => $language_id,
			);
			$shipping_db->join('shipping_items ON shipping_items.shipping_id = shipping.id');
			$shippingList = $shipping_db->where($where)->order('listorder DESC, id ASC')->select();
			if(empty($shippingList)){
				$where = array(
					'status' => 1,
					'language_id' => C('language_id'),
				);
				$shipping_db->join('shipping_items ON shipping_items.shipping_id = shipping.id');
				$shippingList = $shipping_db->where($where)->order('listorder DESC, id ASC')->select();
			}
			foreach ($shippingList as &$shipping){
				if(empty($shipping['title'])){
    			$shipping['title'] = D('shipping_items')->where(array('shipping_id' => $shipping['id'], 'language_id' => C('language_id')))->getField('title');
    		}
				$shipping['area'] = unserialize($shipping['area']);
				$shipping['area_groupid'] = unserialize($shipping['area_groupid']);
				$shipping['firstprice'] = unserialize($shipping['firstprice']);
				$shipping['secondprice'] = unserialize($shipping['secondprice']);
			}
			S($cacheId, $shippingList);
		}
		return $shippingList;
	}
	/**
	 * 获取配送方式信息
	 * @param int $sid 配送方式ID
	 */
	public static function get_shipping_info_by_id( $sid )
	{
		$shippingList = self::get_shipping_list();
		foreach ($shippingList as $shipping){
			if($shipping['id'] == $sid){
				return $shipping;
				break;
			}
		}
		return false;
	}
	
	/**
	 * 获取配送方式信息
	 * @param int $name 配送方式名称
	 */
	public static function get_shipping_info_by_name($name)
	{
		$name = strtolower(trim($name));
		$shippingList = self::get_shipping_list();
		foreach ($shippingList as $shipping){
			if($shipping['name'] == $name){
				return $shipping;
				break;
			}
		}
		return false;
	}
	
	/**
	 * 获取配送费用
	 * @param string $shipping 配送方式ID或者名称
	 * @param int $area_id 地区ID
	 * @param int $goods_amount 商品总额
	 * @param int $weight 商品重量
	 * @return float
	 */
	public static function get_shipping_fee($shipping, $area_id = 0, $goods_amount = 0, $weight = 0,$order_id = 0, $goods_number=0)
	{
		$fee = 0;
		$is_true = 0;	//是否要计算费用
		
//		$shipping_id = intval($shipping_id);
		$area_id = intval($area_id);
//		$total_fee = floatval($total_fee);
		$weight = intval($weight);
		$info = M('orders_items as a')->field('b.catid')->join('left join product as b on a.product_id = b.id')->where('order_id = '.$order_id)->group('b.catid')->select();
                if($info[0]['catid'] == 2121 && count($info) == 1){//充值卡 giftcard 免邮费
                   return 0;
                }
		//获取配送方式信息
		if(is_numeric($shipping)){
			$shippingInfo = self::get_shipping_info_by_id($shipping);
		}else{
			$shippingInfo = self::get_shipping_info_by_name($shipping);
		}
		if(!$shippingInfo || $shippingInfo['status'] == 0){
			return $fee;
		}
		$first_weight = $shippingInfo['first_weight'];	//首重
		$second_weight = $shippingInfo['second_weight'];//续重
		$first_price = $shippingInfo['first_price']; 		//首重费用
		$second_price = $shippingInfo['second_price'];	// 续重费用 
		$free_num = 0;
		//统一设置运费
		if($shippingInfo['price_type'] == 0){
			
			if($shippingInfo['free_price'] > 0 && $goods_amount > $shippingInfo['free_price']){
				return $fee;
			}
			
			$is_true = 1;
		}
		//指定配送地区和费用
		else
		{
			$area_groupid = $shippingInfo['area_groupid'];
			$firstprice = $shippingInfo['firstprice'];
			$secondprice = $shippingInfo['secondprice'];
			$freeprice = unserialize($shippingInfo['freeprice']);
			$freenum = $shippingInfo['freenum'];
			//var_dump($free_price);die;
			if($area_id){
				//查看是否指定了该配送地区
				if(is_array($area_groupid) && $area_groupid)
				{	
					foreach($area_groupid as $area_key => $area_ids)
					{
						if(empty($area_ids)) continue;
						$area_ids = explode(';', $area_ids); 
						//指定了配送地区
						if(in_array($area_id, $area_ids))
						{
							$first_price = isset($firstprice[$area_key]) ? $firstprice[$area_key] : 0;
							$second_price = isset($secondprice[$area_key]) ? $secondprice[$area_key] : 0;
							$free_price = isset($freeprice[$area_key]) ? $freeprice[$area_key] : 0;
							$free_num = isset($freenum[$area_key]) ? $freenum[$area_key] : 0;
							if($free_price > 0 && $goods_amount > $free_price){
								return $fee;
							}
						
							$is_true = 1;
							break;
						}
					}
				}
			}
			
			 //没有指定配送地区但启用了默认费用
			if($is_true==0 && $shippingInfo['open_default'] == 1)
			{
				$is_true = 1;
			}else{
				// TODO 不能配送地区处理
			}
		}
		
		//要计算配送费用
		if($is_true)
		{	
			if($area_id){
				//指定配送地区和费用
				if($shippingInfo['price_type']==1){
				
					if($free_num >0 ){
						if($goods_number){
							$fee = $first_price*$goods_number;
						}
						
					}else{
						//商品重量小于首重量
						if($weight <= $first_weight){
							$fee = $first_price;
						}else{
							$differ_weight = $weight-$first_weight; //商品重量减去首重量的差值
							$fee = $first_price + ceil($differ_weight/$second_weight) * $second_price;
						}
					}
					
				}else{
					//商品重量小于首重量
					if($weight <= $first_weight){
						$fee = $first_price;
					}else{
						$differ_weight = $weight-$first_weight; //商品重量减去首重量的差值
						$fee = $first_price + ceil($differ_weight/$second_weight) * $second_price;
					}
				}
			}else{
				$fee = 0;
			}
			//按件数收费
			/*elseif($shippingInfo['price_type']==2){
				if($goods_number){
					$fee = $first_price * $goods_number;
				}
			}*/
		}else{
			return 'nono';
		}
		return number_format($fee, 2, '.', '');
	}
}